package com.rjwh.dingdong.client.activity.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.MessageListAdapter;
import com.rjwh.dingdong.client.bean.localbean.MessageListItemData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.MessageListPresenter;
import com.rjwh.dingdong.client.mvp.view.MessageListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName MessageListActivity
 * @Description 消息中心
 * @date 2016-12-27
 * @history 2016-12-27 author: description:
 */

public class MessageListActivity extends BaseActivity implements MessageListView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.message_list_listview)
    ListView messageListListview;
    @BindView(R.id.message_list_mSwipyRefreshLayout)
    SwipyRefreshLayout messageListMSwipyRefreshLayout;

    private int pageNumber = 0;
    private MessageListPresenter presenter;
    private MessageListAdapter adapter;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_list_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new MessageListPresenter(this, this);
        messageListMSwipyRefreshLayout.doPullRefreshing();
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getStringExtra("type");
        }
    }

    public static void startActivity(Context context, String type) {
        Intent intent = new Intent(context, MessageListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    private void initView() {
        initToolbarHelper();
        messageListMSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetMessageList(true, SwipyRefreshLayoutDirection.TOP);
                        break;
                    case BOTTOM:
                        pageNumber++;
                        doGetMessageList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
        adapter = new MessageListAdapter(this);
        messageListListview.setAdapter(adapter);
    }

    private void doGetMessageList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        if (TextUtils.isEmpty(type)) {
            type = getString(R.string.message_type_universal);
        }
        presenter.doGetMessageList(type, pageNumber, isRefresh, direction);
    }

    private void initToolbarHelper() {
        toolbar.setTitle("");
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            if (type.equals(LocalConstant.ACTIONURL_HOMEWORK)) {
                getSupportActionBar().setTitle(getString(R.string.homework_mine));
            } else {
                getSupportActionBar().setTitle(R.string.message_center);
            }
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }


    @Override
    public void setRefreshing(boolean isRefresh) {
        messageListMSwipyRefreshLayout.setRefreshing(isRefresh);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        messageListMSwipyRefreshLayout.setDirection(direction);
    }

    @Override
    public void loadData(boolean isRefresh, List<MessageListItemData> sysmessagelist) {
        if (isRefresh) {
            adapter.clearAll();
        }
        adapter.appendToList(sysmessagelist);
    }
}
